'''
Created on Oct 11, 2012

@author: daeheehan

Modified on Oct 31, 2012
@author: gyucheol
'''

from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint
import sys
from types import *

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = 'skswhwo'
GITHUB_USER_ID_4 = 'luispigu'

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = "wasd1234"

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID_2, password=GITHUB_USER_PW)
print gh.ratelimit_remaining, 'request remaining'

# Github NODE Repository information
GITHUB_USER_NAME = "joyent"
GITHUB_REPOSITORY_NAME = "node"

# Oh-my-zsh changer commenter time information file
GRAPH_FILE = 'node_testset_changer_commenter_time.txt'
#GRAPH_FILE = 'rails_changer_commenters4.txt'
#GRAPH_FILE = 'rails_changer_commenters800txt'
#GRAPH_FILE = 'rails_changer_commenters1600.txt'
#GRAPH_FILE = 'rails_changer_commenters2400.txt'
#GRAPH_FILE = 'rails_changer_commenters3200.txt'

NUM_ISSUE_START = 2515
NUM_ISSUE_END = 4335

issue_commenters = []
changer_commenters = {}

try:
    repos = gh.repository(GITHUB_USER_NAME, GITHUB_REPOSITORY_NAME)
    
    # Training-Set(Pull Request number: 4 ~ 4249)
#     Except for No.1336, 1338, 2176 (HTTP 404 Error)
#    for i in range(4, 800):
#    for i in range(800, 1600):
#    for i in range(1600, 2400):
#    for i in range(2400, 3200):
#    for i in range(3200, 4249):
    for i in range(NUM_ISSUE_START, NUM_ISSUE_END):
        try:
            print i
            pull = repos.pull_request(i)
            issue = repos.issue(i)
            
            if (str(pull.is_merged()) == 'False' or str(pull.state) == 'open' or str(pull.is_merged()) == 'False'):
                continue

            # Open or Close, Merged or Not, Created at, Time Taken to Completion, Additon/Deletion/Changed_lines, Pull Requester, Committer(Merged by), Commenters
            list_edgeNode = []
            list_edgeNode.append(', ' + str(pull.state))
            list_edgeNode.append(', ' + str(pull.is_merged()))
            list_edgeNode.append(', ' + pull.created_at.strftime("%Y-%m-%d %H:%M:%S"))
                        
            time_delta =  pull.merged_at - pull.created_at
            delta_seconds = time_delta.days * 86400 + time_delta.seconds
            list_edgeNode.append(', ' + str(delta_seconds))
            sum_of_addition = 0
            sum_of_deletion = 0
#            sum_of_changes = 0
            for c_file in pull.iter_files():
#                print file
#                pprint(vars(c_file))
#                print c_file.additions, c_file.deletions, c_file.changes
                sum_of_addition = sum_of_addition + c_file.additions 
                sum_of_deletion = sum_of_deletion + c_file.deletions
                
            changes_set = str(sum_of_addition) + '/' + str(sum_of_deletion) + '/' + str(sum_of_addition + sum_of_deletion)
                
            list_edgeNode.append(', ' + changes_set)
            list_edgeNode.append(', ' + pull.user.login)
            if (type(pull.merged_by) == NoneType):
                list_edgeNode.append(', None')
            else:
                list_edgeNode.append(', ' + pull.merged_by.login)
#            print pull.user.login, pull.html_url, pull.is_merged(), pull.created_at, pull.state, pull.merged_by.login

#            pprint(vars(pull))
#            print issue.comments
             
            for comment in issue.iter_comments():
#                print comment.user.login
#                print comment
#                pprint(vars(comment))
                list_edgeNode.append(', ' + comment.user.login)

                if(gh.ratelimit_remaining < 100):
                    time.sleep(60 * 60)

            commenters_set = ''.join(list_edgeNode)
            print commenters_set
            changer_commenters[i] = commenters_set
            
            if(gh.ratelimit_remaining < 100):
                time.sleep(60 * 60)

        except Exception as e:
            print 'The inner Exception', e.args[0]
            
#        break
    print 'Printing dictionary: Graph Test Set'
    f_graph = open(GRAPH_FILE, 'a')
#    f_graph.write('Id, Open or Close, Merged or Not, Created at, Time Taken to Completion, Addition/Deletion/Changed_lines, Pull Requester, Committer(Merged by), Commenters \n')
    for key, value in sorted(changer_commenters.items()):
        sys.stdout.write(str(key) + str(value) + '\n')
        f_graph.write(str(key) + str(value) + '\n')

    f_graph.close()

except Exception as e: 
    print 'The final Exception', e.args[0]

print gh.ratelimit_remaining, 'request remaining'

print
print 'Finally the end'
